package metamap.proxy.server.sax;

import java.util.LinkedList;
import java.util.List;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

public class MetamapHandler extends DefaultHandler {
	private List<String> preferredNames;
	private String last = null;
	boolean preferred = false;

	public MetamapHandler() {
		preferredNames = new LinkedList<String>();
		// System.out.println("Handler SAX creato");
	}

	public void startElement(String namespaceURI, String localName,
			String qName, Attributes atts) throws SAXException {
		if (qName.equalsIgnoreCase("CandidatePreferred")) {
			// preferredNames.add(qName);
			// System.out.println(localName+" "+ qName);
			preferred = true;

		}

	}

	public void characters(char[] ch, int start, int length)
			throws SAXException {
		if (preferred) {

			String strValue = new String(ch, start, length);
			// System.out.println("SAX ha trovato preferred: "+ strValue);
			if (!strValue.equalsIgnoreCase(last)) {
				last = strValue;
				preferredNames.add(strValue);
			}

		}
	}

	public void endElement(String namespaceURI, String localName, String qName)
			throws SAXException {
		if (qName.equalsIgnoreCase("CandidatePreferred")) {
			preferred = false;
		}
	}

	public List<String> getPreferred() {
		return preferredNames;
	}
}
